package com.lc.aiagent.app;

import com.lc.aiagent.advisor.MyLoggerAdvisor;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

/**
 * 功能：
 * 作者：lc
 * 日期：2025/5/1 1:37
 */
@Component
@Slf4j
public class LoveClient {
    @Resource
    private VectorStore loveAppVectorStore;

    private static final String LOVE_PROMPT = "你是一个恋爱专家，请根据我的描述，帮我分析一下，我女朋友对我是什么态度？";

    private ChatClient chatClient;
    @Resource
    private Advisor loveAppRagCloudAdvisor;


    public LoveClient(ChatClient.Builder builder) {
        this.chatClient = builder
                .defaultSystem(LOVE_PROMPT)
                .build();

    }

    /**
     * 聊天
     *
     * @param prompt 用户输入
     * @return
     */
    public String doChat(String prompt) {
        ChatResponse chatResponse = chatClient.prompt()
                .user(prompt)
                .call()
                .chatResponse();
        String text = chatResponse.getResult().getOutput().getText();
        // log.info("chat text:{}", text);
        return text;
    }

    /**
     *
     */
    @GetMapping("/doChatRag")
    public String doChatRag(String prompt, String chatId) {
        ChatResponse chatResponse = chatClient.prompt()
                .user(prompt)
                .advisors(advisorSpec -> advisorSpec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                                .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .advisors(new QuestionAnswerAdvisor(loveAppVectorStore))
                .call()
                .chatResponse();
        String content = chatResponse.getResult().getOutput().getText();
        log.info("content: {}", content);
        return content;

    }

    public String doChatWithRag(String message, String chatId) {
        ChatResponse chatResponse = chatClient
                .prompt()
                .user(message)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                // 开启日志，便于观察效果
                .advisors(new MyLoggerAdvisor())
                // 应用增强检索服务（云知识库服务）
                .advisors(loveAppRagCloudAdvisor)
                .call()
                .chatResponse();
        String content = chatResponse.getResult().getOutput().getText();
        log.info("content: {}", content);
        return content;
    }

}
